#!/usr/bin/env python3.9.2
# Version 0.1
# Date:2021/04/12
# Writed By Haonan 图片处理缩小工具
# -*- coding:utf-8 -*-
import os
from PIL import Image

def pictool(readfile):
    print(readfile)
    # 读取图片内容
    pic = Image.open(readfile)
    width, height = pic.size
    print("宽:", width, "高:", height)

    if width > height:
        # 计算倍率，重定width,
        times = float('%.1f' % float(width / height))
        # print("缩放倍率", times)
        finalweight = times * 1000
        # times_height = int(times * 1000)
        print("缩放倍率", finalweight)
        # 缩放至定高1000
        pic = pic.resize((int(finalweight), 1000), Image.ANTIALIAS)
        pic.save(readfile)

    if width < height:
        # 计算倍率，重定width
        times = float('%.1f' % float(height / width))
        print("缩放倍率", times)
        finalist = times * 1000
        # print("缩放倍率", finalist)
        pic = pic.resize((1000, int(finalist)), Image.ANTIALIAS)
        pic.save(readfile)

    if width == height:
        print("1:1缩放")
        pic = pic.resize((1000, 1000), Image.ANTIALIAS)
        pic.save(readfile)
    else:
        print("")
    return


print("图片批处理缩小工具不包含裁剪")

nowdir = os.getcwd()
print(nowdir)
os.chdir(nowdir+"\\image")
dirs = os.listdir(nowdir+"\\image")
imagedir = nowdir+"\\image"
count = 0
for file in dirs:
    pictool(file)



